<script lang="ts" setup>
import { reactive, ref, onMounted, watch } from 'vue'
import type { FormItemProps, FormProps, FormInstance, FormRules } from 'element-plus'
import router from '@/router'

const labelPosition = ref<FormProps['labelPosition']>('top')
const itemLabelPosition = ref<FormItemProps['labelPosition']>('')
const ruleForm = ref({
  phone: '',
  password: ''
})
const ruleFormRef = ref<FormInstance>()
const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入手机号'));
  } else {
    if (!/^1\d{10}$/.test(value)) {
      callback(new Error('手机号应为11位数字，并且首位为1'));
    } else {
      callback();
    }
  }
}
// 2021010513罗玉琼
const checkPass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  }
  callback()
}
const rules = reactive<FormRules<typeof ruleForm>>({
  phone: [{ required: true, validator: checkPhone, trigger: 'blur' }],
  password: [{ required: true, validator: checkPass, trigger: 'blur' }]
})
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('提交成功!')
      router.push({ name: 'IndexView' })
    } else {
      console.log('提交失败!')
    }
  })
}
</script>
<template>
  <div class="box">
    <div class="login">
      <h3>登录</h3>
      <el-form ref="ruleFormRef" :rules="rules" :label-position="labelPosition" :model="ruleForm">
        <el-form-item label="手机号" prop="phone" :label-position="itemLabelPosition">
          <el-input v-model="ruleForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-position="itemLabelPosition">
          <el-input v-model="ruleForm.password" placeholder="请输入密码" show-password type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"> 登录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.box {
  height: 100vh;
  background-image: url('../assets/hxx.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  .login {
    width: 400px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 15px;
    padding: 30px;
    h3 {
      font-size: 25px;
      margin-top: 10px;
      text-align: center;
    }
    .el-form-item--label-top {
      margin-top: 20px;
      .el-input {
        height: 40px;
      }
      .el-button {
        width: 510px;
        height: 40px;
        margin: 25px 0;
        font-size: 15px;
        background-color: #124aace7;
      }
    }
  }
}
</style>
